package com.molichuxing.services.infrastructure.service.impl;


import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.StringUtil;
import com.molichuxing.services.infrastructure.bean.CarsMileageMonitoringEnterpriseBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CarsMileageMonitoringEnterpriseMapper;
import com.molichuxing.services.infrastructure.dao.CarsMileageWarnEnterpriseMapper;
import com.molichuxing.services.infrastructure.dto.request.create.CarsMileageMonitoringEnterpriseDto;
import com.molichuxing.services.infrastructure.dto.response.CarsMileageMonitoringEnterpriseResDto;
import com.molichuxing.services.infrastructure.service.ICarsMileageMonitoringEnterpriseService;
import com.molichuxing.services.property.alarmTabEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 企业订单车辆里程监控表 服务实现类
 * </p>
 *
 * @author wanghualin
 * @since 2020-06-03
 */
@Service("carsMileageMonitoringEnterpriseService")
public class CarsMileageMonitoringEnterpriseServiceImpl implements ICarsMileageMonitoringEnterpriseService {

    @Autowired
    private CarsMileageMonitoringEnterpriseMapper carsMileageMonitoringEnterpriseMapper;
    @Autowired
    private CarsMileageWarnEnterpriseMapper carsMileageWarnEnterpriseMapper;


    @Override
    public Paged<CarsMileageMonitoringEnterpriseResDto> listCarsMileageMonitoring(Map<String, Object> map, Integer pageNum, Integer pageSize) {
        long count = carsMileageMonitoringEnterpriseMapper.countCarsMileageMonitoring(map);
        Paged<CarsMileageMonitoringEnterpriseResDto> dto = new Paged<>();
        dto.setPageNum(pageNum);
        dto.setPageSize(pageSize);
        dto.setTotal(count);
        dto.setTab(getWarn());
        dto.setList(Convert.toListCarsMileageMonitoringDto(carsMileageMonitoringEnterpriseMapper.listCarsMileageMonitoring(map, pageNum, pageSize)));
        return dto;
    }

    private List<TabDto> getWarn() {
        List<TabDto> tabDtos = new ArrayList<>();
        Map map1 = carsMileageWarnEnterpriseMapper.countCarsMileageWarnEnterpriseTab();
        for (alarmTabEnum value : alarmTabEnum.values()) {
            int a=0;
            String s = String.valueOf(value.getValue());
            Object o = map1.get(s);
            if (!StringUtils.isEmpty(o)){
                a= Integer.parseUnsignedInt(o.toString());
            }
            TabDto tabDto = new TabDto(value.getName(), value.getValue(),a);
            tabDtos.add(tabDto);
        }
        return tabDtos;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveCarsMileageMonitoringEnterprise(CarsMileageMonitoringEnterpriseDto dto) {
        CarsMileageMonitoringEnterpriseBean bean = new CarsMileageMonitoringEnterpriseBean();
        BeanUtils.copyProperties(dto, bean);
        int i = carsMileageMonitoringEnterpriseMapper.insertSelective(bean);
        dto.setId(bean.getId());
        return i;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateCarsMileageMonitoringEnterprise(CarsMileageMonitoringEnterpriseDto dto) {
        CarsMileageMonitoringEnterpriseBean bean = new CarsMileageMonitoringEnterpriseBean();
        BeanUtils.copyProperties(dto, bean);
        return carsMileageMonitoringEnterpriseMapper.updateByPrimaryKeySelective(bean);
    }

    @Override
    public CarsMileageMonitoringEnterpriseDto getMileageMonitoringEnterprise(Long order) {

        List<CarsMileageMonitoringEnterpriseBean> beanList = carsMileageMonitoringEnterpriseMapper.getMileageMonitoringEnterprise(order);
        if (CollectionUtils.isEmpty(beanList)) {
            return null;
        } else {
            CarsMileageMonitoringEnterpriseDto dto = new CarsMileageMonitoringEnterpriseDto();
            BeanUtils.copyProperties(beanList.get(0), dto);
            return dto;
        }

    }

    @Override
    public List<CarsMileageMonitoringEnterpriseResDto> listMileageUnMonitoring() {
        return   Convert.toListCarsMileageMonitoringDto(carsMileageMonitoringEnterpriseMapper.listMileageUnMonitoring());
    }
}
